# -*- coding: utf-8 -*-
import math
import random
from pyshare.const.enums.item import ItemType
from app.area.core.component_center import ComponentCenter


class RandomMemorialMixin(object):
    __slots__ = ()
    scene = '11207'
    remark = '批阅奏章'

    def _realize_kind_1(self, _):
        # 金钱增加
        v = int(math.floor(
            random.uniform(3, 6) *
            self.indicators.business *
            self.minister.level * 0.001 + 100
        ))
        self.resources.update_resource_by_type_and_log(ItemType.Silver, v, scene=self.scene, remark=self.remark)
        return 1, v

    def _realize_kind_2(self, _):
        # 粮食增加
        v = int(math.floor(
            random.uniform(3, 6) *
            self.indicators.agricultural *
            self.minister.level * 0.001 + 100
        ))
        self.resources.update_resource_by_type_and_log(ItemType.Food, v, scene=self.scene, remark=self.remark)
        return 2, v

    def _realize_kind_3(self, _):
        # 士兵增加
        v = int(math.floor(
            random.uniform(3, 6) *
            self.indicators.political *
            self.minister.level * 0.001 + 100
        ))
        self.resources.update_resource_by_type_and_log(ItemType.Soldier, v, scene=self.scene, remark=self.remark)
        return 3, v

    @staticmethod
    def _value_from_formula(fx):
        # fx 格式: [[[_, _, v, 1, 20], [_, _, v, 21, 100]]]
        a, b = fx[0][0][-2], fx[0][-1][-1]
        d = random.randint(a, b)
        for _, _, v, x, y in fx[0]:
            if x <= d and d <= y:
                return v
        raise ValueError(fx)

    def _realize_kind_4(self, fx):
        # 1星典籍
        good = self._get_item(fx)
        return 4, good

    def _realize_kind_5(self, fx):
        # 2星典籍
        good = self._get_item(fx)
        return 5, good

    def _realize_kind_6(self, fx):
        # 3星典籍
        good = self._get_item(fx)
        return 6, good

    def _get_item(self, fx):
        # 获得道具
        cc = ComponentCenter()
        award_cfg = cc.goods.get_random_awards(fx)
        return cc.goods.award_many_goods(self.role_id, award_cfg)
